package com.mt.grand.singleton.serialize;

import java.io.*;

public class SerializeSingletonTest {

    public static void main(String[] args) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            SerializeSingleton singleton1 = new SerializeSingleton();
            fos = new FileOutputStream("lazyThreeInstance");
            oos = new ObjectOutputStream(fos);
            oos.writeObject(SerializeSingleton.getInstance());

            fis = new FileInputStream("lazyThreeInstance");
            ois = new ObjectInputStream(fis);
            SerializeSingleton singleton2 = (SerializeSingleton)ois.readObject();

            System.out.println(singleton1.getInstance() == singleton2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                if(ois!=null){
                    ois.close();
                }
                if(fis!=null){
                    fis.close();
                }
                if(oos!=null){
                    oos.close();
                }
                if(fos!=null){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}